package com.situ.datasave;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SharedPreferencesActivity extends AppCompatActivity {
    private static final String TAG = "SharedPreferencesActivi";
    private Button saveDataButton;
    private Button restoreDataButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);
        saveDataButton = (Button) findViewById(R.id.save_data);
        restoreDataButton = (Button) findViewById(R.id.restore_data);
        saveDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  /data/data/com.situ.datasave/shared_prefs/data.xml
                SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                editor.putString("name", "Tom");
                editor.putInt("age", 23);
                editor.putBoolean("married", false);
                editor.apply();
            }
        });
        restoreDataButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
                String name = sharedPreferences.getString("name", "");
                int age = sharedPreferences.getInt("age", 0);
                boolean married = sharedPreferences.getBoolean("married", false);
                Log.d(TAG, "name: " + name);
                Log.d(TAG, "age: " + age);
                Log.d(TAG, "married: " + married);
            }
        });
    }
}
